package com.cspg.design.presenter;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TableLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.cspg.design.CSPGApplication;
import com.cspg.design.MainActivity;
import com.cspg.design.R;
import com.cspg.design.bean.deviceBean.HotLineWorkPlanBean;
import com.cspg.design.dao.gen.DaoSession;
import com.cspg.design.dto.ProjectProcessInformation;
import com.cspg.design.dto.submit.HotLineWork;
import com.cspg.design.request.Project;
import com.cspg.design.ui.gtasks.GTasksActivity;
import com.cspg.design.ui.login.LoginActivity;
import com.cspg.design.ui.project.ProjectHotLineWorkActivity;
import com.cspg.design.widget.CSPGProgressBar;
import com.cspg.design.widget.ToastUtils;

import java.util.List;

public class ProjectPresenter implements IPresenter {
    private final Project project;
    private final Activity activity;
    private CSPGProgressBar cspgProgressBar;
    private TableLayout flowTb;

    private HotLineWorkPlanBean hotLineWorkPlanBean;

    public ProjectPresenter(AppCompatActivity activity) {
        this.project = new Project(this);
        this.activity = activity;
    }

    public void getProjectFlow(String ProjectID, TableLayout tableLayout) {
//        activity.showProgress();
        if (cspgProgressBar == null) {
            cspgProgressBar = CSPGProgressBar.show(activity);
        } else {
            cspgProgressBar.show();
        }
        try {
            this.flowTb = tableLayout;
            project.getProjectFlow(ProjectID);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void getHotLineWork(String projectId) {
        try {
            project.getHotLineWork(projectId);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void updateHotLineWork(HotLineWorkPlanBean hotLineWorkPlanBean) {
        if (cspgProgressBar == null) {
            cspgProgressBar = CSPGProgressBar.show(activity);
        } else {
            cspgProgressBar.show();
        }
        try {
            this.hotLineWorkPlanBean = hotLineWorkPlanBean;
            project.updateHotLineWork(hotLineWorkPlanBean);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void addHotLineWork(HotLineWorkPlanBean hotLineWorkPlanBean) {
        if (cspgProgressBar == null) {
            cspgProgressBar = CSPGProgressBar.show(activity);
        } else {
            cspgProgressBar.show();
        }
        try {
            this.hotLineWorkPlanBean = hotLineWorkPlanBean;
            project.addHotLineWork(hotLineWorkPlanBean);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void netWorkError(String netError) {

        if (null != cspgProgressBar && cspgProgressBar.isShowing()) {
            cspgProgressBar.dismiss();
        }
        ToastUtils.toast(activity, netError);
        if (activity instanceof MainActivity) {
            //activity.openNewActivity(LoginActivity.class);
            Intent intent = new Intent();
            intent.setClass(activity, LoginActivity.class);
            activity.startActivity(intent);
        }
    }

    @Override
    public void httpRequestFailure(String noDataError) {
        if (null != cspgProgressBar && cspgProgressBar.isShowing()) {
            cspgProgressBar.dismiss();
        }
        ToastUtils.toast(activity, noDataError);
        if (activity instanceof MainActivity) {
            //activity.openNewActivity(LoginActivity.class);
            Intent intent = new Intent();
            intent.setClass(activity, LoginActivity.class);
            activity.startActivity(intent);
        }
    }

    @Override
    public void httpRequestSuccess() {
        if (null != cspgProgressBar && cspgProgressBar.isShowing()) {
            cspgProgressBar.dismiss();
        }
        ToastUtils.toast(activity, "成功。。。。");
        this.activity.startActivity(new Intent(activity, GTasksActivity.class));
        activity.finish();
    }

    public void hotLineWorkHttpRequestSuccess(List<HotLineWorkPlanBean> list) {
        if (list == null){
            return;
        }
        for (HotLineWorkPlanBean hotLineWork : list) {
            DaoSession daoSession = ((CSPGApplication) this.activity.getApplication()).getDaoSession();
            daoSession.getHotLineWorkPlanBeanDao().insertOrReplace(hotLineWork);
        }
    }


    public void hotLineWorkUpdateSuccess() {
        if (null != cspgProgressBar && cspgProgressBar.isShowing()) {
            cspgProgressBar.dismiss();
        }
        if (this.hotLineWorkPlanBean != null) {
            DaoSession daoSession = ((CSPGApplication) this.activity.getApplication()).getDaoSession();
            daoSession.getHotLineWorkPlanBeanDao().insertOrReplace(hotLineWorkPlanBean);
            ToastUtils.toast(this.activity, "保存成功......");
        }
    }

    public void projectFlowHttpRequestSuccess(List<ProjectProcessInformation> projectProcessInformationList) {
        if (null != cspgProgressBar && cspgProgressBar.isShowing()) {
            cspgProgressBar.dismiss();
        }
        for (int i = 0; i < projectProcessInformationList.size(); i++) {
            ProjectProcessInformation projectProcessInformation = projectProcessInformationList.get(i);
            View view = LayoutInflater.from(this.activity).inflate(R.layout.layout_flow_item, null);
            TextView orderEt = view.findViewById(R.id.flow_item_order);
            Integer order = i + 1;
            orderEt.setText(order.toString());
            TextView reviewManEt = view.findViewById(R.id.flow_item_review_man);
            reviewManEt.setText(projectProcessInformation.getEmployeeName());
            TextView unitET = view.findViewById(R.id.flow_item_unit);
            unitET.setText(projectProcessInformation.getCompanyName());
            TextView contactEt = view.findViewById(R.id.flow_item_contact);
            contactEt.setText(projectProcessInformation.getEmployeePhoto());
            TextView timeEt = view.findViewById(R.id.flow_item_review_time);
            timeEt.setText(projectProcessInformation.getTimeComment());
            if (this.flowTb != null) {
                this.flowTb.addView(view);
            }
        }
    }
}
